import { GenericEnvStore } from "@/modules/dev-env/store/generic-env-store.ts";
import type { LocalVersionItem } from "@/modules/dev-env/common/types.ts";
import path from "@/framework/util/path.ts";
import type { BaseEnvMirror } from "@/modules/dev-env/mirror/mirror.ts";
import { MavenMirror } from "@/modules/dev-env/java/maven/maven-mirror.ts";
import type { EnvVariableConfig } from "@/modules/dev-env/store/types.ts";

import { EnvTree } from "@/modules/dev-env/common/tree.ts";

/**
 * Maven环境管理
 */
export class MavenEnvStore extends GenericEnvStore<MavenEnvStore> {
  constructor() {
    super("maven");
    this.icon = "icon-maven";
  }

  protected async getVersionInfo(versionDir: string): Promise<LocalVersionItem | undefined> {
    const name = path.basename(versionDir);
    if (name.startsWith("apache-maven-")) {
      const mavenBinFile = path.join(versionDir, "bin/mvn");
      if (await native.fs.exists(mavenBinFile)) {
        const [, , ...omit] = name.split("-");
        return {
          version: omit.join("-"),
          installPath: versionDir,
        };
      }
    }
  }

  getMirrors0(): BaseEnvMirror[] {
    return [
      new MavenMirror("aliyun"),
      new MavenMirror("tencent"),
      new MavenMirror("huaweicloud"),
      new MavenMirror("tsinghua"),
      new MavenMirror("nanjingu"),
      new MavenMirror("apache"),
    ];
  }

  getEnvVariables0(): EnvVariableConfig[] {
    return [
      {
        name: "M2_HOME",
        showCurrent: true,
        settable: true,
        help: "Maven下载jar包存放目录，默认为~/.m2",
      },
      {
        name: "MAVEN_HOME",
        getValue: () => path.join(this.installDir, EnvTree.currentVersionDir),
        showCurrent: true,
        settable: true,
        readonly: true,
        help: "根据设置自动生成，不可修改",
      },
      {
        name: "PATH",
        getValue: () => path.join("%MAVEN_HOME%", "bin"),
        settable: false,
        readonly: true,
        copyName: false,
      },
    ];
  }
}
